<?php
namespace app\index\controller;

use app\common\component\user\UserService;
use app\common\component\user\UserValidate;

class Auth extends ControllerBase {
	public function index() {
		return $this -> view -> fetch();
	}

	public function login() {
		$post = $this -> request -> post();

		$resultUserValidate = (new UserValidate) -> login($post);
		if($resultUserValidate['code'] !== 0) {
			echo '<meta charset="utf-8"><script>alert("' . $resultUserValidate['message'] . '");window.location.href="/login";</script>';exit;
		}

		$loginUser = (new UserService) -> login($post);
		if($loginUser['code'] !== 0) {
			echo '<meta charset="utf-8"><script>alert("' . $loginUser['message'] . '");window.location.href="/login";</script>';exit;
		}
		echo '<meta charset="utf-8"><script>window.location.href="/";</script>';exit;
	}

	public function logout() {
		(new UserService) -> clearLoginSession();
		$this -> redirect('index/Auth/index');
	}
}
?>